const {House, Area, User, School, UserInfo} = require('../db/model/index')
const {Op} = require('sequelize')

const getHouseList = async ({name, address, minRent, maxRent, areaId, uid, isSchool, schoolId, status, createTime, pageIndex, pageSize}) => {
  const where = {
    name: {
      [Op.substring]: name
    },
    address: {
      [Op.substring]: address
    }
  }

  if (uid !== undefined) {
    where.uid = uid
  }

  if (isSchool !== undefined) {
    where.isSchool = isSchool
  }

  if (schoolId !== undefined) {
    where.schoolId = schoolId
  }

  if (status !== undefined) {
    where.status = status
  }

  if (minRent !== undefined && maxRent !== undefined && minRent >= maxRent) {
    maxRent = minRent + 1
  }

  if (minRent !== undefined || maxRent !== undefined) {
    where[`rent`] = {}
  }

  if (minRent !== undefined) {
    where[`rent`][Op.gte] = minRent
  }

  if (maxRent !== undefined) {
    where[`rent`][Op.lte] = maxRent
  }

  if (createTime.length) {
    where[`created_at`] = {
      [Op.gte]: createTime[0],
      [Op.lte]: createTime[1]
    }
  }

  if (areaId.length) {
    where[`areaId`] = {
      [Op.in]: areaId
    }
  }

  const result = await House.findAndCountAll({
    limit: +pageSize,
    offset: pageSize * pageIndex,
    order: [
      [ 'id', 'desc' ]
    ],
    include: [
      {
        model: Area,
        attributes: [ 'name' ]
      },
      {
        model: User,
        as: 'user',
        attributes: [ 'nickName' ]
      },
      {
        model: School,
        attributes: [ 'name' ]
      }
    ],
    where
  })

  return {
    count: result.count,
    data: result.rows
  }
}

const getHouseListMy = async ({name, address, minRent, maxRent, areaId, uid, isSchool, schoolId, status, createTime, pageIndex, pageSize}) => {
  const where = {
    name: {
      [Op.substring]: name
    },
    address: {
      [Op.substring]: address
    }
  }

  if (uid !== undefined) {
    where.uid = uid
  }

  if (isSchool !== undefined) {
    where.isSchool = isSchool
  }

  if (schoolId !== undefined) {
    where.schoolId = schoolId
  }

  if (status !== undefined) {
    where.status = status
  }

  if (minRent !== undefined && maxRent !== undefined && minRent >= maxRent) {
    maxRent = minRent + 1
  }

  if (minRent !== undefined || maxRent !== undefined) {
    where[`rent`] = {}
  }

  if (minRent !== undefined) {
    where[`rent`][Op.gte] = minRent
  }

  if (maxRent !== undefined) {
    where[`rent`][Op.lte] = maxRent
  }

  if (createTime.length) {
    where[`created_at`] = {
      [Op.gte]: createTime[0],
      [Op.lte]: createTime[1]
    }
  }

  if (areaId.length) {
    where[`areaId`] = {
      [Op.in]: areaId
    }
  }

  const result = await House.findAndCountAll({
    limit: +pageSize,
    offset: pageSize * pageIndex,
    order: [
      [ 'id', 'desc' ]
    ],
    include: [
      {
        model: Area,
        attributes: [ 'name' ]
      },
      {
        model: User,
        as: 'user',
        attributes: [ 'nickName' ]
      },
      {
        model: User,
        as: 'askUser',
        attributes: [ 'nickName' ]
      },
      {
        model: School,
        attributes: [ 'name' ]
      }
    ],
    where
  })

  return {
    count: result.count,
    data: result.rows
  }
}

const   getHouseOneMy = async (id) => {
  const result = await House.findOne({
    where: {
      id
    },
    include: [
      {
        model: Area,
        attributes: [ 'name' ]
      },
      {
        model: User,
        as: 'user',
        attributes: [ 'nickName' ]
      },
      {
        model: User,
        as: 'askUser',
        attributes: [ 'nickName' ],
        include: [
          {
            model: UserInfo
          }
        ]
      },
      {
        model: School,
        attributes: [ 'name' ]
      }
    ]
  })

  return result.dataValues
}

const getHouseOne = async (id) => {
  const result = await House.findOne({
    where: {
      id
    },
    include: [
      {
        model: Area,
        attributes: [ 'name' ]
      },
      {
        model: User,
        as: 'user',
        attributes: [ 'nickName' ]
      },
      {
        model: School,
        attributes: [ 'name' ]
      }
    ]
  })

  return result.dataValues
}

const createHouse = async ({name, address, rent, imgs, description, areaId, uid, isSchool, schoolId, status}) => {
  const result = await House.create({
    name,
    address,
    rent,
    areaId,
    uid,
    isSchool,
    schoolId,
    imgs,
    description,
    status
  })

  const data = result.dataValues

  return data
}

const updateHouse = async ({name, address, rent, imgs, askId, description, areaId, isSchool, schoolId, status, id}) => {
  const whereOpt = {
    id
  }
  const result = await House.update({
    name,
    address,
    rent,
    imgs,
    description,
    areaId,
    isSchool,
    schoolId,
    status,
    askId
  }, {
    where: whereOpt
  })

  const data = result.length > 0

  return data
}

const delHouse = async (id) => {
  const result = await House.destroy({
    where: {
      id
    }
  })

  return result > 0
}

module.exports = {
  createHouse,
  updateHouse,
  delHouse,
  getHouseList,
  getHouseOne,
  getHouseListMy,
  getHouseOneMy
}
